<?php

/**
 * 
 * found at: http://php.net/manual/de/language.oop5.patterns.php
 * 
 * Talraith 12-Mar-2009 01:42
 * I think a simpler option is to have a loader class/object and then the actual class.  
 * It keeps your class more secure in that you are defining the visibility on $instances and 
 * you keep your class neat and tidy. 
 *
 * I found this useful when trying to use the singleton method on a class that extends another class. 
 *
 */

class module_loader { 
    private static $instances; 

    public static function getInstance($module_name) { 
        if (!isset(self::$instances[$module_name])) { 
            self::$instances[$module_name] = new $module_name(); 
        } 

        return self::$instances[$module_name]; 
    } 
} 

class module_base { 
    protected $name; 

    public function __construct() { 
        // Do initialization stuff here 

    } 

    public final function SomePermanentFunction() { 
        // Perform function actions 
    } 
} 

class module_test extends module_base { 
    public $my_var; 

    public function __construct() { 
        parent::__construct(); 

        $this->name = 'test'; 
        $this->my_var = ''; 
    } 

    public function ModuleSpecificFunction() { 

    } 
} 


// So then once you have all of that defined, you can then use it like such: 

// Get first instance and set value 
$instance1 = module_loader::getInstance('module_test'); 
$instance1->my_var = 'abc'; 

print "Instance 1 Var = " . $instance1->my_var; 

// Create second instance and check value 
$instance2 = module_loader::getInstance('module_test'); 

print "Instance 2 Var = " . $instance2->my_var; 

// Set value on second instance, check first instance 
$instance2->my_var = 'def'; 

print "Instance 1 Var = " . $instance1->my_var; 

//The output would then look like this: 
//
//Instance 1 Var = abc 
//Instance 2 Var = abc 
//Instance 1 Var = def 
//
//Thus $instance1 and $instance2 are created using the factory pattern but 
//they both point back to the same object (singleton pattern).



/**
 * 
 * 
 * 
 */




/**
 * 
 * 
 * 
 */




/**
 * 
 * 
 * 
 */




/**
 * 
 * 
 * 
 */





?>
